package com.goldgov.mobile;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.goldgov.column.service.Column;
import com.goldgov.column.service.ColumnInit;
import com.goldgov.column.service.ColumnQuery;
import com.goldgov.column.service.ColumnService;
import com.goldgov.column.service.UserColumn;
import com.goldgov.column.service.UserColumnQuery;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.utils.PrintUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 
 * @author liziwen
 *
 */
@RestController
@RequestMapping("/mobile/open/column")
@Api("栏目免过滤接口")
public class ColumnForMobileOpenController {

	@Autowired
	private ColumnService columnService;
	
	/**
	 * 获取所有栏目，并查出用户所选的栏目
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@GetMapping("/getUserColumn")
	@ApiOperation(value = "查询用户栏目" )
	public JsonQueryObject<Column> getUserColumn(ColumnQuery query , HttpServletRequest request,Model model){
		LoginAuthUser user=LoginAuthUser.getUser(request);
		//固定栏目
		Integer[] arraycolumn= {ColumnQuery.COLUMN_DJTT,ColumnQuery.COLUMN_ZSJJH,ColumnQuery.COLUMN_GCDT,ColumnQuery.COLUMN_ZYJS,ColumnQuery.COLUMN_GNDJYW,ColumnQuery.COLUMN_QNDJYW};
		if(user!=null) {
			UserColumnQuery userColumnQuery=new UserColumnQuery();
			userColumnQuery.setSearchUserId(user.getEntityID());
			userColumnQuery.setSearchType(UserColumnQuery.COLUMN_COL);
			userColumnQuery.setPageSize(-1);
			List<UserColumn> ucList =  columnService.findUserColumn(userColumnQuery);
			if(ucList!=null&& ucList.size()>0) {
				//不为空时加上关联userid
				for(Column column:ColumnInit.getColumnList()) {
					for(UserColumn userColumn:ucList) {
						if(column.getCategoryId().equals(userColumn.getColumnId())) {
							column.setUserId(userColumn.getUserId());
							column.setIsFocus(Column.FOCUS_YES);
							break;
						}else {
							column.setIsFocus(Column.FOCUS_NO);
						}
					}
				}
			}else {
				//为空时固定的栏目加userid
				for(Column column:ColumnInit.getColumnList()) {
					for(Integer col:arraycolumn) {
						if(col.equals(column.getCategoryId())) {
							column.setUserId(user.getEntityID());
							column.setIsFocus(Column.FOCUS_YES);//关注
							break;
						}else {
							column.setIsFocus(Column.FOCUS_NO);
						}
					}
				}
				//将固定栏目加入关联表中
				ColumnQuery columnQuery=new ColumnQuery();
				columnQuery.setSearchAddColumnIds(arraycolumn);
				columnQuery.setSearchUserID(user.getEntityID());
				columnQuery.setSearchType(UserColumnQuery.COLUMN_COL);
				columnService.upDateUserColumn(columnQuery);
			}
			query.setResultList(ColumnInit.getColumnList());
		}else {
		//用户未登录下
		List<Column> list=new ArrayList<Column>();
			for(Column column:ColumnInit.getColumnList()) {
				for(Integer col:arraycolumn) {
					if(col.equals(column.getCategoryId())) {
						list.add(column);
						break;
					}
				}
			}
			query.setResultList(list);
		}
		
		return new JsonQueryObject<Column>(query);
	}
	/**
	 * 更新用户栏目
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@PostMapping("/updateUserColumn")
	@ApiOperation(value = "更新用户栏目" , notes = "传入参数为：栏目id数组")
	public void updateUserColumn(Integer[] columnIds,HttpServletRequest request,Model model,HttpServletResponse response){
		LoginAuthUser user=LoginAuthUser.getUser(request);
		//LoginAuthUser user=LoginAuthUser.getUserTest(request);
		
		if(columnIds != null&&user!=null &&columnIds.length>0 ) {
			try {
				ColumnQuery query =new ColumnQuery();
				query.setSearchAddColumnIds(columnIds);
				query.setSearchUserID(user.getEntityID());
				query.setSearchType(UserColumnQuery.COLUMN_COL);
				columnService.upDateUserColumn(query);
				model.addAttribute("msg", "更新成功");
				PrintUtil.print(response, true);;
			} catch (Exception e) {
				e.printStackTrace();
				model.addAttribute("msg", "更新失败,请联系管理员");
				PrintUtil.print(response, false);;
			}
		}else {
			model.addAttribute("msg", "更新失败");
			PrintUtil.print(response, false);
		}
	}

}
